//
//  GoodsV1Cell.m
//  jipin
//
//  Created by lcb on 6/12/15.
//  Copyright (c) 2015 liuchengbin. All rights reserved.
//

#import "GoodsV1Cell.h"
#import "EXPhotoViewer.h"
#import "UIButtonView.h"
#import "ZUIImageView.h"
@interface GoodsV1Cell()
{
    
}
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) ZUIImageView *pimageView;
@property(nonatomic,strong)RTLabel *skuNameLabel;
@property(nonatomic,strong)UIView *skuNameLine;
@property(nonatomic,strong)RTLabel *skuNameContent;
@property(nonatomic,strong)RTLabel *cateLabel;
@property(nonatomic,strong)RTLabel *brandLabel;
@property(nonatomic,strong)UIButton *recommendbtn;

@end


@implementation GoodsV1Cell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        _bgView = [[UIView alloc] initWithFrame:CGRectZero];
        _bgView.backgroundColor =[UIColor clearColor];
        [self.contentView addSubview:_bgView];
        //
        UITapGestureRecognizer* singleRecognizer= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom)];
        _pimageView =[[ZUIImageView alloc] initWithFrame:CGRectZero];
        _pimageView.backgroundColor =UIColorFromRGB(0xffffff);
        _pimageView.userInteractionEnabled =YES;
        
        _pimageView.contentMode = UIViewContentModeScaleAspectFit;
        [_pimageView addGestureRecognizer:singleRecognizer];
        [_bgView addSubview:_pimageView];
        singleRecognizer=nil;
        //
        
//        UITapGestureRecognizer* singleRecognizer1= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom)];
        _skuNameLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        _skuNameLabel.backgroundColor = [UIColor clearColor];
        _skuNameLabel.textColor = [UIColor darkGrayColor];
        _skuNameLabel.textAlignment = NSTextAlignmentLeft;
        _skuNameLabel.font = FONT(16);
        _skuNameLabel.lineSpacing = LSpacing;
        _skuNameLabel.userInteractionEnabled =YES;
//        [_skuNameLabel addGestureRecognizer:singleRecognizer1];
        [_bgView addSubview:_skuNameLabel];
        
        _skuNameLine = [[UIView alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH, 0, PHOTO_FRAME_WIDTH*3, 1)];
        _skuNameLine .backgroundColor =[UIColor lightGrayColor];
        [_bgView addSubview:_skuNameLine];
        //
//        UITapGestureRecognizer* singleRecognizer2= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom)];
        _skuNameContent =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        _skuNameContent.backgroundColor = [UIColor clearColor];
        _skuNameContent.textColor = [UIColor darkGrayColor];
        _skuNameContent.textAlignment = NSTextAlignmentLeft;
        _skuNameContent.font = FONT(14);
        _skuNameContent.lineSpacing = LSpacing;
        _skuNameContent.userInteractionEnabled =YES;
//        [_skuNameContent addGestureRecognizer:singleRecognizer2];
        [_bgView addSubview:_skuNameContent];
        //
        UITapGestureRecognizer* singleRecognizer3= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom3)];
        _cateLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        _cateLabel.backgroundColor = [UIColor clearColor];
        _cateLabel.textColor = UIColorFromRGB(0x285790);
        _cateLabel.textAlignment = NSTextAlignmentLeft;
        _cateLabel.font = FONT(14);
        _cateLabel.lineSpacing = LSpacing;
        _cateLabel.userInteractionEnabled =YES;
        [_cateLabel addGestureRecognizer:singleRecognizer3];
        [_bgView addSubview:_cateLabel];
        singleRecognizer3=nil;
        
        UITapGestureRecognizer* singleRecognizer4= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom4)];
        _brandLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        _brandLabel.backgroundColor = [UIColor clearColor];
        _brandLabel.textColor = UIColorFromRGB(0x285790);
        _brandLabel.textAlignment = NSTextAlignmentLeft;
        _brandLabel.font = FONT(14);
        _brandLabel.lineSpacing = LSpacing;
        _brandLabel.userInteractionEnabled =YES;
        [_brandLabel addGestureRecognizer:singleRecognizer4];
        [_bgView addSubview:_brandLabel];
        singleRecognizer4=nil;
        
        _recommendbtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _recommendbtn.backgroundColor =[UIColor whiteColor];
        _recommendbtn.layer.borderColor=UIColorFromRGB(0x285790).CGColor;
        _recommendbtn.layer.borderWidth =0.5;
        _recommendbtn.layer.cornerRadius = 3;
        _recommendbtn.layer.masksToBounds=YES;
        [_recommendbtn setTitleColor:UIColorFromRGB(0x285790) forState:UIControlStateNormal];
        _recommendbtn.titleLabel.font = FONT(14);
        [_recommendbtn addTarget:self action:@selector(bangdanAction) forControlEvents:UIControlEventTouchUpInside];
        [_recommendbtn setTitle:@"推荐到榜单" forState:UIControlStateNormal];
        [_bgView addSubview:_recommendbtn];
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock=nil;
    self.data=nil;
    self.bgView=nil;
    self.pimageView=nil;
    
    self.skuNameLabel=nil;
    self.skuNameContent =nil;
    self.recommendbtn=nil;
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _bgView.frame =CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    
}
- (void)drawRect:(CGRect)rect {

    [super drawRect:rect];
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model other:(id)other key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.other =other;
    [self showInfo:model indexPath:indexPath];
}
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key = key;
    [self showInfo:model indexPath:indexPath];
}
- (void)showInfo:(id)model indexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    self.indexPath =indexPath;
    float fTop = 0;
    fTop += PHOTO_FRAME_WIDTH ;
    NSString *skuname =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];

    if (skuname.length>0) {
        [_skuNameLabel setText:[NSString stringWithFormat:@"%@",skuname]];
        CGSize optimumSize = [_skuNameLabel optimumSize];
        _skuNameLabel.frame = CGRectMake(PHOTO_FRAME_WIDTH,fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, optimumSize.height);
        _skuNameLine.frame = CGRectMake(PHOTO_FRAME_WIDTH,_skuNameLabel.frame.origin.y+_skuNameLabel.frame.size.height+5, PHOTO_FRAME_WIDTH*2, 1);
        fTop = fTop+optimumSize.height+PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING;
    }
    
    NSString *cat =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"cat2"]] replaceNullString];
    [_cateLabel setText:cat];
    CGSize optimumSize10 = [_cateLabel optimumSize];
    CGFloat hh0 =optimumSize10.height;
    if (hh0<20) {
        hh0=20;
    }
    _cateLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH,fTop,optimumSize10.width, hh0);
    
    NSString *brand =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"brand"]] replaceNullString];
    if(brand.length>0 && cat.length>0)
    {
        brand =[NSString stringWithFormat:@"  /  %@",brand];
    }else
    {
        brand =[NSString stringWithFormat:@"%@",brand];
    }
    if (brand.length>0) {
        [_brandLabel setText:brand];
        CGSize optimumSize11 = [_brandLabel optimumSize];
        CGFloat hh =optimumSize11.height;
        if (hh<20) {
            hh=20;
        }
        _brandLabel.frame =CGRectMake(_cateLabel.frame.origin.x+_cateLabel.frame.size.width,fTop-1,optimumSize11.width, hh);
        fTop = fTop+hh+PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING;
    }else{
        fTop = fTop+hh0+PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING;
    }
    
    //
    NSString * strURL = [NSString stringWithFormat:@"%@", [dict objectForKey:@"largePicURL"]];
    if (strURL!=nil&&strURL.length>0) {
        _pimageView.frame = CGRectMake(PHOTO_FRAME_WIDTH, fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2 ,DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2);
        _pimageView.imageUrl = strURL;
        _pimageView.placeholderImage =[UIImage imageNamed:@"image.png"];
    }
    fTop = _pimageView.frame.origin.y+_pimageView.frame.size.height+PHOTO_FRAME_WIDTH*2;
    //
   
    NSString *text =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]] replaceNullString];

    if (text.length>0) {
        text =[NSString stringWithFormat:@"“%@”",text];
        [_skuNameContent setText:text];
        CGSize optimumSize1 = [_skuNameContent optimumSize];
        _skuNameContent.frame = CGRectMake(PHOTO_FRAME_WIDTH,fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, optimumSize1.height);
        fTop = fTop+optimumSize1.height+PHOTO_FRAME_WIDTH*2;
    }
 
    //
    _recommendbtn.frame = CGRectMake(PHOTO_FRAME_WIDTH,fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, MORE_BUTTON_HEIGHT+4);
    [self layoutSubviews];
}


/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary*)model;
    NSString *skuname =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
    CGFloat hight =0.0;
    if (skuname.length>0) {
        RTLabel *text1Label =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        text1Label.font = FONT(16);
        text1Label.lineSpacing =LSpacing;
        [text1Label setText:[NSString stringWithFormat:@"%@",skuname]];
        CGSize optimumSize = [text1Label optimumSize];
        CGFloat hh =optimumSize.height;
        if (hh<20) {
            hh=20;
        }
        hight = hight +hh+PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING;
    }else{
        hight =PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING;
    }
    NSString *cat =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"cat2"]] replaceNullString];
    NSString *brand =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"brand"]] replaceNullString];
    if (brand.length>0) {
        cat =[NSString stringWithFormat:@"%@ / %@",cat,brand];
    }
    if (cat.length>0) {
        RTLabel *text2Label =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        text2Label.font = FONT(14);
        text2Label.lineSpacing = LSpacing;
        [text2Label setText:cat];
        CGSize optimumSize2 = [text2Label optimumSize];
        CGFloat hh =optimumSize2.height;
        if (hh<20) {
            hh=20;
        }
        hight = hight +hh+PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING;
    }else{
        hight = hight +PHOTO_FRAME_WIDTH*2+PHOTO_TIME_PADDING;
    }
    //
    NSString *text =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]] replaceNullString];
    if (text.length>0) {
        text =[NSString stringWithFormat:@"“%@”",text];
        
        RTLabel *textLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        textLabel.font = FONT(14);
        textLabel.lineSpacing = LSpacing;
        [textLabel setText:text];
        CGSize optimumSize1 = [textLabel optimumSize];
         textLabel=nil;
        CGFloat hh =optimumSize1.height;
        if (hh<20) {
            hh=20;
        }
        hight =hight + hh+PHOTO_FRAME_WIDTH*2;
    }
    hight=hight +MORE_BUTTON_HEIGHT+(DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2)+PHOTO_FRAME_WIDTH*3;
    return hight;
    
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)categoryAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_category,sender,self.data,self.key,self.indexPath);
    }
}
-(void)brandAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_brand,sender,self.data,self.key,self.indexPath);
    }
}
-(void)goodAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_good,sender,self.data,self.key,self.indexPath);
    }
}
-(void)likeAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_like,sender,self.data,self.key,self.indexPath);
    }
}
-(void)commentAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_comment,sender,self.data,self.key,self.indexPath);
    }
}
-(void)handleSingleTapFrom
{
   [EXPhotoViewer showImageFrom:_pimageView.iconView];
}
-(void)handleSingleTapFrom3
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_category,nil,self.data,self.key,self.indexPath);
    }
}
-(void)handleSingleTapFrom4
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_brand,nil,self.data,self.key,self.indexPath);
    }
}
-(void)bangdanAction
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_bangdan,nil,self.data,self.key,self.indexPath);
    }
}
@end
